﻿using AutoMapper;
using Modules;
using PmSoft;

namespace ManagementApi.Models.System.Flow
{
    public class FlowDto
    {
        /// <summary>
        /// 主键 
        ///</summary>
        public int Id { get; set; }
        /// <summary>
        /// 流程编码 
        ///</summary>
        public string Code { get; set; }
        /// <summary>
        /// 流程名称 
        ///</summary>
        public string Name { get; set; }
        /// <summary>
        /// 创建时间 
        ///</summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 动作集合
        /// </summary>
        public IEnumerable<FlowEngineDto> flowEngineDtos { get; set; }
    }

    /// <summary>
    /// 流程动作集合
    /// </summary>
    public class FlowEngineDto
    {
        /// <summary>
        /// 步骤编码 
        ///</summary>
        public string Code { get; set; }
        /// <summary>
        /// 步骤名称 
        ///</summary>
        public string Name { get; set; }
        /// <summary>
        /// 步骤顺序 
        ///</summary>
        public int? Sort { get; set; }
    }

    public static partial class FlowExceptions
    {
        public static FlowDto AsDto(this SysFlow flow, IEnumerable<SysFlowEngine> sysFlowEngines)
        {
            var mapper = ServiceAccessor.GetService<IMapper>();
            FlowDto flowDto = new FlowDto();
            mapper.Map(flow, flowDto);
            List<FlowEngineDto> flowEngines = new List<FlowEngineDto>();
            if (sysFlowEngines.Count() > 0)
            {
                IEnumerable<SysFlowEngine> sysFlowEngines1 = sysFlowEngines.Where(a => a.FlowId == flow.Id);
                mapper.Map(sysFlowEngines1, flowEngines);
                flowDto.flowEngineDtos = flowEngines;
            }
            return flowDto;
        }
    }
}
